
package com.csh.szhm.custorm;


import com.csh.szhm.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class CustomProgressDialog extends Dialog
{

	private static CustomProgressDialog customProgressDialog = null;


	public CustomProgressDialog(Context context)
	{
		super(context);
	}

	public CustomProgressDialog(Context context, int theme)
	{

		super(context, theme);
		setListener();
	}

	public static CustomProgressDialog createDialog(Context context)
	{
		customProgressDialog =
				new CustomProgressDialog(context, R.style.dialog);
		customProgressDialog.setContentView(R.layout.load_progress);
		Window window = customProgressDialog.getWindow();
		WindowManager.LayoutParams params = window.getAttributes();
		params.width = params.WRAP_CONTENT;
		params.height = params.WRAP_CONTENT;
		params.gravity = Gravity.CENTER;
		window.setAttributes(params);
		return customProgressDialog;
	}

	public void onWindowFocusChanged(boolean hasFocus)
	{

		if (customProgressDialog == null)
		{
			return;
		}

		ImageView imageView =
				(ImageView) customProgressDialog
						.findViewById(R.id.loadingImageView);
		setAnimation(imageView);
	}

	private void setAnimation(View view)
	{
		view.clearAnimation();
		Animation util =
				AnimationUtils.loadAnimation(getContext(), R.anim.rote);
		view.setAnimation(util);
		util.start();
	}


	public void setTouchCencalVisable(boolean flag)
	{
		this.setCanceledOnTouchOutside(flag);
	}

	private void setListener()
	{
		this.setCanceledOnTouchOutside(false);
		this.setOnCancelListener(new OnCancelListener()
		{

			@Override
			public void onCancel(DialogInterface dialog)
			{
				if (mProGress != null)
				{
					mProGress.dissmis();
				}
			}
		});
		this.setOnDismissListener(new OnDismissListener()
		{

			@Override
			public void onDismiss(DialogInterface dialog)
			{
			}
		});
	}

	private onCencalProGress mProGress;

	/**
	 * 用于返回直接退出
	 * @param progress
	 */
	public void setOnCancelProGress(onCencalProGress progress)
	{
		this.mProGress = progress;
	}

	public static interface onCencalProGress
	{

		public void dissmis();
	}

}